package com.lxf.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleDriver;

import com.microsoft.sqlserver.jdbc.SQLServerDriver;

public class MyConnection {
	public Connection conn = null;
	
	/**
	 * get dbConnection
	 * @param type		oracel, sqlserver, mysql
	 * @param dbUrl		
	 * @param username	
	 * @param password	
	 * @return	conn	
	 */
	public Connection getConnection(String type, String dbUrl, String username, String password){
		try {
			if("oracle".equals(type.toLowerCase())){
				DriverManager.registerDriver(new OracleDriver());
			}else if("sqlerver".equals(type.toLowerCase())){
				DriverManager.registerDriver(new SQLServerDriver());
			}else if("mysql".equals(type.toLowerCase())){
				
			}
			conn = DriverManager.getConnection(dbUrl, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * closeConnection
	 * @param conn
	 */
	public void closeConnection(Connection conn){
		try {
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * closeConnection
	 * @param conn
	 * @param ps
	 * @param rs
	 */
	public void closeConnection(Connection conn, PreparedStatement ps, ResultSet rs){
		try {
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}
	
}
